package com.erci.controller.admin;

import com.erci.entity.DTO.UserDeleteDTO;
import com.erci.entity.DTO.UserUpdateDTO;
import com.erci.entity.User;
import com.erci.result.PageResult;
import com.erci.result.Result;
import com.erci.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin(origins = "http://localhost:5173")
@RestController
@RequestMapping("/admin/api/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    // 分页查询
    @GetMapping("/list")
    public Result<PageResult> list(Integer page, Integer pageSize,String keyword) {
        log.info("用户页面分页查询");
        PageResult pageResult =userService.queryList(page,pageSize, keyword);

        return Result.success(pageResult);
    }
    //新增
    @PostMapping("/add")
    public Result<String> add(@RequestBody User user) {
        log.info("用户新增");
        userService.add(user);
        return Result.success("用户新增成功");
    }
    //删除
    @PostMapping("/delete")
    public Result<String> delete(@RequestBody UserDeleteDTO userDeleteDTO) {
        log.info("用户删除");
        userService.delete(userDeleteDTO);
        return Result.success("用户删除成功");
    }
    //修改
    @PostMapping("/update")
    public Result<String> update(@RequestBody UserUpdateDTO userUpdateDTO  ) {
        log.info("用户修改");
        userService.update(userUpdateDTO);
        return Result.success("用户修改成功");
    }
}
